package dp.ua.epic.pages;

import epic.domain.Hero;
import epic.domain.PathDecision;
import epic.services.HeroService;
import java.io.Serializable;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;

/**
 * Class that responcible for 'Fate' page
 * 
 * @author Chris
 * @date 30.01.2011 18:11:52
 */
@MountPath(path = "fate")
public class Fate extends BasePage {

    @SpringBean
    private HeroService heroService;

    public Fate() {
        if (isHeroWithoutFate()) {
            setRedirect(true);
            setResponsePage(Choose.class);
        }
        
        add(new ListView("heroes", new Model() {

            @Override
            public Serializable getObject() {
                return (Serializable) heroService.getAllHeros();
            }
        }) {

            @Override
            protected void populateItem(ListItem item) {
                Hero hero = (Hero) item.getModelObject();
                item.add(new Label("heroName", hero.getHeroName()));              
                item.add(new Label("heroFate", new ResourceModel(hero.getDecision().toString())));
            }
        });

    }
    
    private boolean isHeroWithoutFate() {
        return getEpicSession().getHero().getDecision() == PathDecision.UNDEFINED;
    }
}
